package com.rockwell.robot2015;

import com.rockwell.robot2015.model.ColorBlobInfo;
import com.rockwell.robot2015.model.OnColorBlobDetectListener;
import com.rockwell.robot2015.views.MazeCameraView;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;

public class DetectColorActivity extends Activity {
	private MazeCameraView	m_mazeCameraView;

	private OnColorBlobDetectListener m_treasureListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(boolean present, ColorBlobInfo info, int length,
				int width) {
			enableCheckbox(R.id.checkbox_treasure, present);
		}
	};

	private OnColorBlobDetectListener m_obstacleListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			enableCheckbox(R.id.checkbox_obstacle, present);
		}
	};

	private OnColorBlobDetectListener m_mazeEntranceGuideListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			enableCheckbox(R.id.checkbox_entrance_guide, present);
		}
	};

	private OnColorBlobDetectListener m_dropAreaListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			enableCheckbox(R.id.checkbox_droparea, present);
		}
	};

	private OnColorBlobDetectListener m_outerWallListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			enableCheckbox(R.id.checkbox_outer_wall, present);
		}
	};

	private OnColorBlobDetectListener m_maze1Listener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			enableCheckbox(R.id.checkbox_maze1, present);
		}
	};

	private OnColorBlobDetectListener m_maze2Listener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			enableCheckbox(R.id.checkbox_maze2, present);
		}
	};

	private OnColorBlobDetectListener m_maze3Listener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			enableCheckbox(R.id.checkbox_maze3, present);
		}
	};


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detect_color);

		m_mazeCameraView = (MazeCameraView) this.findViewById(R.id.maze_camera_view);
		m_mazeCameraView.setTeachMode(false);
		
		m_mazeCameraView.setTreasureListener(m_treasureListener);
		//m_mazeCameraView.setObstacleListener(m_obstacleListener);
		m_mazeCameraView.setMaze1Listener(m_maze1Listener);
		m_mazeCameraView.setMaze2Listener(m_maze2Listener);
		m_mazeCameraView.setMaze3Listener(m_maze3Listener);
		m_mazeCameraView
				.setMazeEntranceGuideListener(m_mazeEntranceGuideListener);
		m_mazeCameraView.setOuterWallListener(m_outerWallListener);
		m_mazeCameraView.setDropAreaListener(m_dropAreaListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detect_color, menu);
		return true;
	}

	@Override
	public void onResume() {
		super.onResume();
		m_mazeCameraView.onActivated();
	}

	@Override
	public void onPause() {
		super.onPause();
		m_mazeCameraView.onDeactivated();
	}

	private void enableCheckbox(final int id, final boolean present) {
		DetectColorActivity.this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				CheckBox cb = (CheckBox)findViewById(id);
				cb.setChecked(present);
			}
		});
	}
}
